﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

namespace Jy123.Web.Module.Venues
{
    public class VenuesRaceManage : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            string strVId = context.Request.QueryString["VenuesId"];
            if (string.IsNullOrEmpty(strVId))
            {
                strVId = context.Request["VenuesId"];
            }
            string type = context.Request.QueryString["Type"];
            if (string.IsNullOrEmpty(type))
            {
                type = context.Request["Type"];
            }
            string strScript = "<script>$moonbox.warning('您已经开始比赛了！')</script>";
            if (!string.IsNullOrEmpty(strVId))
            {
                BLL.Race.Race BLLRace = new Jy123.BLL.Race.Race();
                BLL.Venues_Main BLLVenues = new Jy123.BLL.Venues_Main();
                BLL.Club_Main BLLClub = new Jy123.BLL.Club_Main();
                Model.Venues_Main venues = BLLVenues.GetModel(int.Parse(strVId));
                BLL.Public_JType BLLJType = new Jy123.BLL.Public_JType();
                Model.Public_JType JType = BLLJType.GetModel(venues.FJType.Value);
                bool isCar = (JType.FSportType.Value == Convert.ToInt32(BLL.Public.EnumConfig.SportType.Car) || JType.FSportType.Value == Convert.ToInt32(BLL.Public.EnumConfig.SportType.F1));
                string originalId = context.Request.Form["selClubHidden"];
                string strRedirect = string.Format("/Module/Venues/VenuesRace.aspx?VenuesId={0}&Time={1}", strVId, DateTime.Now.ToLongDateString());
                if (isCar)
                {
                    strRedirect = string.Format("/Module/CarVenues/CarRace.aspx?VenuesId={0}&Time={1}", strVId, DateTime.Now.ToLongDateString());
                }

                switch (type.ToLower())
                {
                    case "join":
                        string strCId = originalId;
                        Model.Club_Main club = BLLClub.GetModel(int.Parse(strCId));
                        if (venues.VStatus.Value == Convert.ToInt32(BLL.Public.EnumConfig.VenuesStatus.Empty) || venues.VStatus.Value == Convert.ToInt32(BLL.Public.EnumConfig.VenuesStatus.Wait))
                        {
                            BLLRace.JoinVenues(venues, club);
                        }
                        context.Response.Redirect(strRedirect);
                        break;
                    case "startrace":
                        if (venues.VStatus.Value == Convert.ToInt32(BLL.Public.EnumConfig.VenuesStatus.Empty) || venues.VStatus.Value == Convert.ToInt32(BLL.Public.EnumConfig.VenuesStatus.Wait))
                        {
                            if (!isCar)
                            {
                                string[] strClubId = originalId.Split('_');
                                Model.Club_Main club1 = BLLClub.GetModel(int.Parse(strClubId[0]));
                                Model.Club_Main club2 = BLLClub.GetModel(int.Parse(strClubId[1]));
                                BLLRace.StartRace(venues, club1, club2);
                            }
                            else
                            {
                                venues.RaceClub = originalId;
                                BLLVenues.Update(venues);
                                BLLRace.StartRace(venues);
                            }
                        }
                        else
                        {
                            context.Response.Write(strScript);
                        }
                        context.Response.Redirect(strRedirect);
                        break;
                    case "endrace":
                        string strLocationScript = string.Format("<script>window.location='/Module/RaceResult.aspx?VenuesId={0}';</script>", strVId);
                        context.Response.Write(strLocationScript);
                        break;
                    case "quit":
                        string strId = venues.RaceClub;
                        string strVScript = string.Format("/Module/Venues/VenuesRace.aspx?VenuesId={0}&Time={1}", strVId, DateTime.Now.ToLongDateString());
                        string strCScript = string.Format("/Module/Club/MyClub.aspx?ClubId={0}", strId);
                        if (isCar)
                        {
                            strVScript = string.Format("/Module/CarVenues/CarRace.aspx?VenuesId={0}&Time={1}", strVId, DateTime.Now.ToLongDateString());
                            strCScript = string.Format("/Module/CarClub/MyClub.aspx?ClubId={0}", strId);
                        }
                        if (venues.VStatus.Value == Convert.ToInt32(BLL.Public.EnumConfig.VenuesStatus.Wait))
                        {
                            BLLRace.QuitVenues(venues);
                            context.Response.Redirect(strCScript);
                        }
                        else
                        {
                            context.Response.Write(strScript);
                            context.Response.Redirect(strVScript);
                        }

                        break;
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
